package com.couldtest.serverConsumerRibbon.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


/**
 * Created by liguanfu on 2018/6/7.
 */
@RestController
public class DcController {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    LoadBalancerClient loadBalancerClient;

    @GetMapping("/consumer")
    public String dc() {
        return restTemplate.getForObject("http://provider-a/dc", String.class);
    }

    @GetMapping("/consumerA")
    public String dcA() {
        ServiceInstance provider = loadBalancerClient.choose("provider");
        RestTemplate restTemplate = new RestTemplate();
        String url = String.format("http:/%s:$s", provider.getHost(), provider.getPort());
        return this.restTemplate.getForObject(url, String.class);
    }

}
